<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: HTMLPortlet.php 402 2011-01-04 21:18:51Z steen.rabol $
 * @package Pradoportal.Portlets.HTMLPortlet.Edit
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.HTMLPortlet.Common.HTMLPortletRecord');

class HTMLPortlet extends Portlet
{
	public function getPortletRecord()
	{
		if(is_null(parent::getPortletRecord()))
		{
			$this->PortletRecord = HTMLPortletRecord::finder()->findByPk($this->Parameters['id']);
			if(is_null(parent::getPortletRecord()))
			{
				$this->PortletRecord			= new HTMLPortletRecord();
				$this->PortletRecord->id		= $this->Parameters['id'];
				$this->PortletRecord->css		= 'htmlportlet';
				$this->PortletRecord->culture	= $this->Application->Culture;
				$this->PortletRecord->save();
			}
		}
		return parent::getPortletRecord();
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->getPage()->IsPostBack)
		{
			$this->getPortletRecord();
			$this->HtmlContent->Text	= $this->PortletRecord->content;
			$this->HTMLPortletCSS->Text	= $this->PortletRecord->css;
			$this->HtmlContent->setWidth("100%");
		}
	}

	public function SaveContent($sender,$param)
	{
		if(is_null($this->PortletRecord))
		{
			$this->PortletRecord			= new HTMLPortletRecord();
			$this->PortletRecord->id		= (isset($this->Parameters['id']) ? $this->Parameters['id'] : 0);
		}

		$this->PortletRecord->content	= $this->HtmlContent->Text;
		$this->PortletRecord->css		= $this->HTMLPortletCSS->Text;
		$this->PortletRecord->culture	= $this->Application->Culture;
		$this->PortletRecord->save();
		$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
	}

	public function CancelContent($sender,$param)
	{
		if($this->PortletRecord)
		{
			$this->HtmlContent->Text	= $this->PortletRecord->content;
			$this->HTMLPortletCSS->Text	= $this->PortletRecord->css;
		}
		else
		{
			$this->HtmlContent->Text	= "";
			$this->HTMLPortletCSS->Text	= "htmlportlet";
		}
	}

	public function DeleteButtonClicked($sender,$param)
	{
		$this->PortletRecord->delete();
		parent::DeleteButtonClicked($sender,$param);
	}

	public function getCssPath()
	{
		return $this->Request->BaseUrl . '/themes/Portal/' . $this->Application->Parameters['ThemeName'] . '/style.css';
	}
}

?>